/*
 * sanity2.c
 *
 *  Created on: Jun 13, 2014
 *      Author: eran
 */

#include "types.h"
#include "stat.h"
#include "user.h"

#define FLAG 0x202

int
main(int argc, char *argv[])
{
	char *path, *pass;
	char buf[100];
	int fd, fail;
	//int fail, fd;
	if (argc != 3){
		printf(2, "usage: sanity2 pathname password\n");
		exit();
	}
	path = argv[1];
	pass = argv[2];
	/*
	fd = open(path, FLAG);
	printf(1, "%s is now open\n", path);
	fail = fprot(path, pass);
	printf(1, "Locking of %s %s\n", path, fail == -1 ? "failed" : "succeeded");
	close(fd);
	printf(1, "%s is now closed\n", path);
	fail = fprot(path, pass);
	printf(1, "Locking of %s %s\n", path, fail == -1 ? "failed" : "succeeded");
	fd = open(path, FLAG);
	printf(1, "Opening locked %s %s\n", path, fd == -1 ? "failed" : "succeeded");
	pass[0] = '7';
	fail = funprot(path, pass);
	printf(1, "Unlocking %s %s\n", path, fail == -1 ? "failed" : "succeeded");
	if(fd == -1)
		fd = open(path, FLAG);
	printf(1, "Opening locked %s %s\n", path, fd == -1 ? "failed" : "succeeded");
	if(fd != -1)
		close(fd);*/
	fprot(path, pass);
	if(fork()==0){
		fail = funlock(path, pass);
		printf(1, "Unlocking %s %s\n", path, fail == -1 ? "failed" : "succeeded");
		fd = open(path, 0);
		read(fd, buf, 100);
		printf(1, "%s\n", buf);
		exit();
	}
	wait();
	fd = open(path, 0);
	if(fd == -1)
		printf(1, "Failed to open file\n");
	fail = funprot(path, pass);

	printf(1, "Unprotecting %s %s\n", path, fail == -1 ? "failed" : "succeeded");
	if (fd!=-1)
		close(fd);
	exit();
}

